package com.higgins.lawapp.server.dataItemModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jdo.annotations.Element;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.persistence.Embedded;

import com.google.appengine.api.datastore.Key;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemContainerDTO;
import com.higgins.lawapp.client.settings.dataItemModel.DataItemDTO;

@PersistenceCapable
public class DataItemContainer {
	@SuppressWarnings("unused")
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String url;
	
	@Persistent
	private Date collectionTimestamp;
	
	@Persistent
	private String dataSource;
	
	@Persistent
	private String dataLevel;
	
	@Persistent
	private String dataInstance;
	
	@Persistent
	private String coveringLetter;
	
	@Persistent
	private Boolean apply;
	
	@Persistent(defaultFetchGroup = "true")
	private List<DataItem> dataItems;
	
	@Persistent
	@Element(dependent = "true")
	private List<String> dataItemTitles;
	
	public Long getId()	{
		return id;
	}
	public void setURL(String url)	{
		this.url = url;
	}
	public String getURL()	{
		return url;
	}
	public void setCollectionTimestamp(Date collectionTimestamp)	{
		this.collectionTimestamp = collectionTimestamp;
	}
	public Date getCollectionTimestamp()	{
		return collectionTimestamp;
	}
	public void setDataSource(String dataSource)	{
		this.dataSource = dataSource;
	}
	public void setDataLevel(String dataLevel)	{
		this.dataLevel = dataLevel;
	}
	public void setDataInstance(String dataInstance)	{
		this.dataInstance = dataInstance;
	}
	public void setDataItems(List<DataItem> dataItems)	{
		this.dataItems = dataItems;
	}
	public void setDataItemTitles(List<String> dataItemTitles)	{
		this.dataItemTitles = dataItemTitles;
	}
	
	
	public String getDataSource()	{
		return dataSource;
	}
	public String getDataLevel()	{
		return dataLevel;
	}
	public String getDataInstance()	{
		return dataInstance;
	}
	public String getCoveringLetter()	{
		return coveringLetter;
	}
	public void setCoveringLetter(String coveringLetter)	{
		this.coveringLetter = coveringLetter;
	}
	public Boolean getApply()	{
		return apply;
	}
	public void setApply(Boolean apply)	{
		this.apply = apply;
	}
	public List<DataItem> getDataItems()	{
		return dataItems;
	}
	public List<String> getDataItemTitles()	{
		return dataItemTitles;
	}
	
	public List<DataItemDTO> getDataItemDTOs()	{
		List<DataItemDTO> dataItemDTOs = new ArrayList<DataItemDTO>();
		if (dataItems!=null)	{
			Iterator<DataItem> iterator = dataItems.iterator();
			while(iterator.hasNext())	{
				DataItem dataItem = iterator.next();
				DataItemDTO dataItemDTO = dataItem.getDataItemDTO();
				dataItemDTOs.add(dataItemDTO);
			}
		}	else 	{
			System.out.println("dataItems is empty!");
		}
		return dataItemDTOs;
	}
	
	public void setDataItemDTOs(List<DataItemDTO> dataItemDTOs)	{
		dataItems = new ArrayList<DataItem>();
		Iterator<DataItemDTO> iterator = dataItemDTOs.iterator();
		while(iterator.hasNext())	{
			DataItemDTO dataItemDTO = iterator.next();
			DataItem dataItem = new DataItem();
			dataItem.setDataItemDTO(dataItemDTO);
			dataItems.add(dataItem);
		}
	}
	
	public DataItemContainerDTO getDataItemContainerDTO()	{
		DataItemContainerDTO dataItemContainerDTO = new DataItemContainerDTO();
		dataItemContainerDTO.setDataSource(getDataLevel());
		dataItemContainerDTO.setDataLevel(getDataLevel());
		dataItemContainerDTO.setDataInstance(getDataInstance());

		dataItemContainerDTO.setDataItems(getDataItemDTOs());
		
		Iterator<String> iterator = getDataItemTitles().iterator();
		int index = 0;
		while(iterator.hasNext())	{
			String title = iterator.next();
			switch(index)	{
			case 0:
				dataItemContainerDTO.setDataTitleOne(title);
				break;
			case 1:
				dataItemContainerDTO.setDataTitleTwo(title);
				break;
			case 2:
				dataItemContainerDTO.setDataTitleThree(title);
				break;
			case 3:
				dataItemContainerDTO.setDataTitleFour(title);
				break;
			case 4:
				dataItemContainerDTO.setDataTitleFive(title);
				break;
			}
			index++;
		}
		return dataItemContainerDTO;
	}

	public void setDataItemContainerDTO(DataItemContainerDTO dataItemContainer)		{
		dataSource = dataItemContainer.getDataSource();
		dataLevel = dataItemContainer.getDataLevel();
		dataInstance = dataItemContainer.getDataInstance();
		setDataItemDTOs(dataItemContainer.getDataItems());
//		dataItemTitles = dataItemContainer.getDataItemTitles();
		dataItemTitles = new ArrayList<String>();
		String title = dataItemContainer.getDataTitleOne();
		if (title!=null)
			dataItemTitles.add(title);
		title = dataItemContainer.getDataTitleTwo();
		if (title!=null)
			dataItemTitles.add(title);
		title = dataItemContainer.getDataTitleThree();
		if (title!=null)
			dataItemTitles.add(title);
		title = dataItemContainer.getDataTitleFour();
		if (title!=null)
			dataItemTitles.add(title);
		title = dataItemContainer.getDataTitleFive();
		if (title!=null)
			dataItemTitles.add(title);
	}
}
